/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciko.code.adapter.WorkHotActionListAdapter;
import com.ciko.code.bean.GetHotActivitiesListEty;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.http.Req.GetHotActivitiesListReq;
import com.ciko.code.views.PullListView;
import com.mywork.activity.WebActivity;
import com.mywork.activity.WorkActionActivity;
import com.mywork.activity.WorkManagerActivity;
import com.mywork.activity.WorkMeetingActivity;
import com.mywork.activity.WorkOfficeActivity;
import com.mywork.activity.WorkPlaneActivity;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000227.R;
import com.tgx.tina.android.ipc.framework.IUIPage;

import java.util.ArrayList;
import java.util.List;

import base.tina.core.task.infc.ITaskResult;


/**
 * 文件名：WorkIndexPage
 * 描    述：我的工作模块
 * 作    者：司海林
 * 时    间：2015/12/20
 * 版    权：
 **/
public class WorkIndexPage extends AbstractUIPage<MainActivity> implements OnClickListener, AdapterView.OnItemClickListener, HttpResponseListener {

    private static WorkIndexPage instance;
    private TextView tvTitle;
    private ImageView ivPersion;
    private RelativeLayout btnBack;
    private View rect_managerWork;
    private View rect_actionWork;
    private View rect_planeWork;
    private View rect_officWork;
    private View rect_meetingWork;
    private PullListView lv_hotActionWork;
    private WorkHotActionListAdapter workHotActionListAdapter;
    private List<GetHotActivitiesListEty> getHotActivitiesListEtyList = new ArrayList<>();

    public WorkIndexPage(MainActivity mainActivity) {
        super(mainActivity);
    }

    public static WorkIndexPage getInstance(MainActivity t) {
        if (instance == null) {
            instance = new WorkIndexPage(t);
        } else {
            instance.setActivity(t);
        }
        return instance;
    }

    public static void destroy() {
        if (instance != null) {
            instance.dispose();
            instance = null;
        }
    }

    @Override
    public <E extends Activity> View createView(E e, int i) {

        curMyView = bActivity.getLayoutInflater().inflate(R.layout.fragment_work, null);

        initTitleView();
        initView();
        initListener();
        initAdapter();

        return curMyView;
    }

    private void initAdapter() {
        workHotActionListAdapter = new WorkHotActionListAdapter(context, getHotActivitiesListEtyList, R.layout.item_list_work_hot_action);

        lv_hotActionWork.setAdapter(workHotActionListAdapter);
    }

    private void initView() {
        rect_managerWork = curMyView.findViewById(R.id.rect_managerWork);
        rect_actionWork = curMyView.findViewById(R.id.rect_actionWork);
        rect_planeWork = curMyView.findViewById(R.id.rect_planeWork);
        rect_officWork = curMyView.findViewById(R.id.rect_officWork);
        rect_meetingWork = curMyView.findViewById(R.id.rect_meetingWork);
        lv_hotActionWork = (PullListView) curMyView.findViewById(R.id.lv_hotActionWork);
    }

    private void initTitleView() {
        tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
        tvTitle.setText("我的工作");
        ivPersion = (ImageView) curMyView.findViewById(R.id.common_header_persion);
        ivPersion.setVisibility(View.VISIBLE);
        btnBack = (RelativeLayout) curMyView.findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.GONE);
    }

    protected void initListener() {
        rect_managerWork.setOnClickListener(this);
        rect_actionWork.setOnClickListener(this);
        rect_planeWork.setOnClickListener(this);
        rect_officWork.setOnClickListener(this);
        rect_meetingWork.setOnClickListener(this);

        ivPersion.setOnClickListener(this);

        lv_hotActionWork.setOnItemClickListener(this);
    }

    @Override
    public View updateView(int i, ITaskResult iTaskResult) {
        return null;
    }

    @Override
    public void notifyView(int i, Bundle bundle) {

    }

    @Override
    public int enter(IUIPage<?> iuiPage) {
        doRequestHttp(new GetHotActivitiesListReq(0, 100));
        return 0;
    }

    @Override
    public int leave(IUIPage<?> iuiPage) {
        return 0;
    }

    @Override
    public void setStatus(int i) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rect_managerWork:
                intent2Acticity(WorkManagerActivity.class);
                break;
            case R.id.rect_actionWork:
                intent2Acticity(WorkActionActivity.class);
                break;
            case R.id.rect_planeWork:
                intent2Acticity(WorkPlaneActivity.class);
                break;
            case R.id.rect_officWork:
                intent2Acticity(WorkOfficeActivity.class);
                break;
            case R.id.rect_meetingWork:
                intent2Acticity(WorkMeetingActivity.class);
                break;
            case R.id.common_header_persion:
                bActivity.goPersonalActivity();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        GetHotActivitiesListEty getHotActivitiesListEty = (GetHotActivitiesListEty) parent.getAdapter().getItem(position);

        if (getHotActivitiesListEty.getHasHtml().equals("y")) {
            Intent intent = new Intent(context, WebActivity.class);
            intent.putExtra("URL", getHotActivitiesListEty.getHtmlUrl());
            context.startActivity(intent);
        }
    }

    @Override
    public void onRequestStart(String tag) {

    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        switch (tag) {
            case "GetHotActivitiesListReq":
                List<GetHotActivitiesListEty> getHotActivitiesListEtyListResult = (List<GetHotActivitiesListEty>) response.getReturnObject();
                getHotActivitiesListEtyList.clear();
                getHotActivitiesListEtyList.addAll(getHotActivitiesListEtyListResult);
                workHotActionListAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {

    }
}
